import greenfoot.*;

/**
 * This class defines a crab. Crabs live on the beach.
 */
public class Crab extends Actor
{
    GreenfootImage image1 = new GreenfootImage("crab.png");
    GreenfootImage image2 = new GreenfootImage("crab2.png");
    int count=0;
    
    public Crab(){
        setImage(image1);
    }
    
    public void act()
    {
        keyControl();
        eat();
        switchImage();
        
    }
    
    public void switchImage(){
        if(getImage() == image1){
            setImage(image2);
        } else setImage(image1);
    }
    
    public void keyControl(){
        if(Greenfoot.isKeyDown("left")){
            turn(-3);
            move(3);
        }
        if(Greenfoot.isKeyDown("right")){
            turn(3);
            move(3);
        }
    }
    
    public void eat(){
        if(isTouching(Worm.class)) {
            removeTouching(Worm.class);
            Greenfoot.playSound("slurp.wav");
            count++;
            World w = getWorld();
            w.showText("吃虫数:" + count,50,15);
            GreenfootImage bg = w.getBackground();
            //GreenfootImage bg = getBackground();
            //Font font1 = bg.getFont();
            if(count == 3) {
                bg.drawString("成功！",200,300);
                Greenfoot.stop();
            }
        }
    }
    
    public void eatlist() {
        World w = getWorld();
        GreenfootImage bg = w.getBackground();
        Color yanse;
        yanse = bg.getColor();
        yanse = Color.GREEN;
        bg.setColor(yanse);
        Font font1 = bg.getFont();
        font1 = font1.deriveFont(150);
        bg.setFont(font1);  
           
    }
}


